package Demo01;

public class Main {
    public static void main(String[] args) {

    }
}

class Solution {
    public static void main(String[] args) {
//        int[] a = {3, 6, 7, 11};
        int[] a = {30,11,23,4,20};

        System.out.println(minEatingSpeed(a, 6));
    }
    public static int minEatingSpeed(int[] piles, int h) {
        // 范围 : [1, max]
        int l = 1;
        int r = 0;
        for (int i = 0; i < piles.length; i++) {
            r = Math.max(r, piles[i]);
        }
        int res = 0;
        while (l<=r) {
            int m = l + ((r - l) >> 1);
            long ans = getHour(m, piles);
            //你一个小时吃完了, 但是要求2小时, 时间够了, 速度还可以再小
            if(ans <= h) {
                res = m;
                r = m - 1;
            }else {
                l = m + 1;
            }
        }
        return res;
    }

    private static long getHour(int speed, int[] piles) {
        long sum = 0;
        for (int i = 0; i < piles.length; i++) {
            sum += piles[i]/speed;
            if(piles[i]%speed!=0) {
                sum++;
            }
        }
        return sum;
    }
}